﻿@{
    
Layout ="~/_Sitelayout.cshtml";


  var roleName = "";
  string[] userNames = new string[1];
  string[] roleNames = new string[1];
 
  // Need to manually query users because Membership.GetAllUsers is not working
  var db = Database.Open("UserDB");
  var selectQueryString = "SELECT UserId, Email FROM UserProfile";
  var Query = db.Query(selectQueryString);
 WebGrid UserGrid  = new WebGrid(Query,columnNames: new[] {"UserId","Email"});
  if(IsPost){
 
    // Create new role
    if(!Request["buttonCreateRole"].IsEmpty()){
      roleName=Request["textRoleName"];
      if(!Roles.RoleExists(roleName) && !roleName.IsEmpty()){
        Roles.CreateRole(roleName);
      }
    } // if(buttonCreateRole)
   
    // Delete role
    if(!Request["buttonDeleteRole"].IsEmpty()){
     
      roleName=Request["textRoleName"];
      if(Roles.GetUsersInRole(roleName).Length == 0 && !roleName.IsEmpty())
      {
        Roles.DeleteRole(roleName, true); // true means throw if any users are in this role
      }
    } // if(buttonDeleteRole)
   
   
    // Add user to role
    if(!Request["buttonAddUserToRole"].IsEmpty()){
      userNames[0] = Request["selectUserName"];
      roleNames[0] = Request["selectRoleName"];
      if(!Roles.IsUserInRole(userNames[0], roleNames[0])){
        Roles.AddUsersToRoles(userNames, roleNames);
      }
    } // if(buttonAddUserToRole)
   
   
    // Delete user from role
    if(!Request["buttonDeleteUserFromRole"].IsEmpty()){
      userNames[0] = Request["selectUserName"];
      roleNames[0] = Request["selectRoleName"];
      if(Roles.IsUserInRole(userNames[0], roleNames[0])){
        Roles.RemoveUsersFromRoles(userNames, roleNames);
      }
    } // if(buttonDeleteUseFromRole)
  }
}
  
<!DOCTYPE html>
<html lang="en">
    <head>
      <link rel="stylesheet" href="~/styles/stylesheet.css" type="text/css" />
      <meta charset="utf-8" />
        <title>Manage Roles</title>
        <style type="text/css">
          fieldset{width:92%;}
        </style>
    </head>
    <body>
      <h1>Manage Roles</h1>
      <form method="post">
       
        <fieldset>
          <label for="textRoleName">Role name:</label><br/>
          <input type="text" value="" name="textRoleName" /> &nbsp;
          <input type="submit" value="Create Role" name="buttonCreateRole" />
          <input type="submit" value="Delete Role" name="buttonDeleteRole" />
        </fieldset>
       
        <p></p>
 
        <fieldset>
          <label for="selectUserName">Users:</label>
          <select name="selectUserName">
            @foreach(var row in db.Query(selectQueryString)) {
              <option>@row.Email</option>
            }
          </select>
          &nbsp;
          <label for="selectRoleName">Roles:</label>
          <select name="selectRoleName">
            @foreach(var r in Roles.GetAllRoles())
            {
              <option>@r</option>
            }
          </select>
          &nbsp;
          <input type="submit" value="Add User to Role" name="buttonAddUserToRole" />
          <input type="submit" value="Delete User from Role" name="buttonDeleteUserFromRole" />
        </fieldset>
     
         
      <h2>Roles</h2>   
          <ul>
          @foreach(var r in Roles.GetAllRoles())
          {
            <li>@r</li>
            <ul>
            @foreach(var u in Roles.GetUsersInRole(r))
            {
              <li>@u</li>
            }
            </ul>
          }
          </ul>
        <p><a href="@Href("~/Default")">Return to home page</a></p>
        
      </form>
       
    </body>
</html>

